using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using Microsoft.Xna.Framework.Media;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class UserQRCodeCtrl : UserControl
	{
		private const string TAG = "UserQRCodeCtrl";
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatchQRCode;
		private object mObjectWatcher;
		private bool mBIsShownQRCode;
		private UITaskDlgEx mDlgDoWithFather;
		internal Button btnLayoutRoot;
		internal Image imgQRCode;
		private bool _contentLoaded;
		public UserQRCodeCtrl()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("UserQRCodeCtrl", "Control_Loaded");
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("UserQRCodeCtrl", "Control_Unloaded");
		}
		private void registerEvent()
		{
			if (this.mWatchQRCode == null)
			{
				this.mWatchQRCode = new EventWatcher(this, null, new EventHandlerDelegate(this.OnQRCode));
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GET_QRCODE_SUCCESS, this.mWatchQRCode);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GET_QRCODE_ERR, this.mWatchQRCode);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchQRCode != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GET_QRCODE_SUCCESS, this.mWatchQRCode);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GET_QRCODE_ERR, this.mWatchQRCode);
				this.mWatchQRCode = null;
			}
		}
		private void OnQRCode(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			switch (evtArgs.mEventID)
			{
			case EventConst.ON_NETSCENE_GET_QRCODE_SUCCESS:
				if (this.mDlgDoWithFather != null)
				{
					this.mDlgDoWithFather(UserQRCodeOp.OpKind_DownLoadEnd);
				}
				this.refreshQRCode();
				return;
			case EventConst.ON_NETSCENE_GET_QRCODE_ERR:
				if (this.mDlgDoWithFather != null)
				{
					this.mDlgDoWithFather(UserQRCodeOp.OpKind_DownLoadEnd);
					MMMessageBox.Show(strings.Reg_PhoneName_MsgBadNet, true, "确定");
				}
				return;
			default:
				return;
			}
		}
		private void btnLayoutRoot_Click(object sender, RoutedEventArgs e)
		{
			this.downLoadQRCode();
		}
		private void downLoadQRCode()
		{
			if (this.mDlgDoWithFather != null)
			{
				this.mDlgDoWithFather(UserQRCodeOp.OpKind_DownLoadStart);
			}
			ServiceCenter.sceneGetQRCode.doScene();
		}
		private void disPlayQRCode()
		{
			if (this.mBIsShownQRCode)
			{
				return;
			}
			this.mBIsShownQRCode = true;
			BitmapImage qRImg = ServiceCenter.sceneGetQRCode.getQRImg();
			if (qRImg != null)
			{
				this.imgQRCode.set_Source(qRImg);
				return;
			}
			this.downLoadQRCode();
		}
		private void refreshQRCode()
		{
			this.mBIsShownQRCode = false;
			this.disPlayQRCode();
		}
		public void Init(UITaskDlgEx uitaskDlgEx_0)
		{
			this.mDlgDoWithFather = uitaskDlgEx_0;
			this.disPlayQRCode();
			this.registerEvent();
		}
		public void UnInit()
		{
			this.mDlgDoWithFather = null;
			this.unRegisterEvent();
		}
		public void SavaPic()
		{
			if (this.imgQRCode.get_Source() == null)
			{
				return;
			}
			if ((BitmapImage)this.imgQRCode.get_Source() == null)
			{
				return;
			}
			byte[] array = StorageIO.readFromFile(ServiceCenter.sceneGetQRCode.getQRFileName());
			if (array == null)
			{
				return;
			}
			MemoryStream memoryStream = new MemoryStream(array);
			if (memoryStream == null)
			{
				return;
			}
			try
			{
				MediaLibrary mediaLibrary = new MediaLibrary();
				mediaLibrary.SavePicture(this.GetExisPicName(), memoryStream);
				MessageBoxManager.ShowToast(strings.ImageViewSaveSuccess, 2500, null, false);
			}
			catch (Exception ex)
			{
				Log.smethod_1(base.GetType().get_Name(), "SavaPic Exception" + ex.get_StackTrace());
				MMMessageBox.Show(strings.ImageViewSaveFail, true, "确定");
			}
		}
		private string GetExisPicName()
		{
			return "MicroMsgQRCode" + DateTime.get_Now().get_Ticks() + ".jpg";
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/QRCode/UI/UserQRCodeCtrl.xaml", 2));
			this.btnLayoutRoot = (Button)base.FindName("btnLayoutRoot");
			this.imgQRCode = (Image)base.FindName("imgQRCode");
		}
	}
}
